import "package:flutter/material.dart";
import "package:myapp/widget/keepAliveWrapper.dart";
import "../_views/dynamic/video.dart";
import '../_views/dynamic/subscribe.dart';

class SwDynamicPage extends StatefulWidget {
  const SwDynamicPage({super.key});
  @override
  State<StatefulWidget> createState() {
    return _SwDynamicPageState();
  }
}

class _SwDynamicPageState extends State<SwDynamicPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  final List<TabsItem> _tabs = [
    TabsItem(label: '视频', value: '0'),
    TabsItem(label: '关注', value: '1'),
  ];
  Color tabActiveColor = Colors.black;
  int tabActive = 1; //当前选中
  final double _statusBarHeight = 44.0;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
      length: _tabs.length,
      vsync: this,
      initialIndex: tabActive,
    );
    _tabController.addListener(
      () {
        tabActive = _tabController.index;
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Scaffold(
        body: Column(
          children: [
            Container(
              width: double.infinity,
              decoration: const BoxDecoration(
                color: Color.fromARGB(255, 234, 187, 243),
                image: DecorationImage(
                    image: AssetImage(
                      "./images/1.jpg",
                    ),
                    fit: BoxFit.cover,
                    opacity: 0.35),
              ),
              child: Column(
                children: [
                  SizedBox(
                    height: _statusBarHeight,
                  ),
                  TabBar(
                    controller: _tabController,
                    tabs: _tabs.map((e) => Text(e.label)).toList(),
                    indicatorColor: tabActiveColor,
                    labelColor: tabActiveColor,
                    unselectedLabelColor: const Color(0xff000000),
                    unselectedLabelStyle: const TextStyle(
                      fontSize: 15,
                    ),
                    labelStyle: const TextStyle(
                      fontSize: 15,
                      fontWeight: FontWeight.bold,
                    ),
                    labelPadding: const EdgeInsets.all(10),
                    tabAlignment: TabAlignment.center,
                  ),
                ],
              ),
            ),
            Expanded(
              flex: 1,
              child: TabBarView(
                controller: _tabController,
                children: [
                  KeepAliveWrapper(
                    child: DynamicVideo(),
                  ),
                  KeepAliveWrapper(
                    child: DynamicSubscribe(),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class TabsItem {
  String label;
  String value;
  bool loaded;
  TabsItem({
    required this.label,
    required this.value,
    this.loaded = false,
  });
}
